#!/usr/bin/python
import sys
import os
import os.path
import smtplib
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEAudio import MIMEAudio
from email.MIMEImage import MIMEImage
from email.Encoders import encode_base64
from email import encoders

from config import *

def send(subject, fname):
  global user,passwd,recipient,mailserver,mailport 

  msg = MIMEMultipart()
  msg['From'] = user
  msg['To'] = recipient
  msg['Subject'] = subject
  file = MIMEBase('application','octet-stream')
  f = open(fname,'rb')
  file.set_payload(f.read())
  f.close()
  encoders.encode_base64(file)
  file.add_header('Content-Disposition', 'attachment', filename=subject)
  msg.attach(file)

  mailServer = smtplib.SMTP(mailserver, mailport)
  mailServer.ehlo()
  mailServer.starttls()
  mailServer.ehlo()
  mailServer.login(user, passwd)
  mailServer.sendmail(user, recipient.split(','), msg.as_string())
  mailServer.close()
  
  print('Sent email to %s' % recipient)

